package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repostiory.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时先别管事务
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository repository;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public Travel get(String id) {
        Travel travel = repository.findById(id).orElse(null);
        if (travel != null) {
            travel.setAuthor(userInfoService.getOne(travel.getUserId()));
            Destination dest = destinationService.getOne(travel.getDestId());
            travel.setDest(dest);

        }
        return travel;
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        Query query = new Query();
        if (qo.getState() == 1) {
            query.addCriteria(Criteria.where("state").is(qo.getState()));
        }
        if (qo.getState() == -1) {
            query.addCriteria(Criteria.where("state").is(Travel.STATE_REJECT));
        }
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //天数
        TravelCondition day = qo.getDay();
        if (day!=null){
            query.addCriteria(Criteria.where("day").gte(day.getMin())
            .lte(day.getMax()));
        }
        //钱
        TravelCondition perExpend = qo.getPerExpend();
        if (perExpend!=null){
            query.addCriteria(Criteria.where("perExpend").gte(perExpend.getMin())
                    .lte(perExpend.getMax()));
        }
        Pageable pageable =null;
        if (qo.getOrderType()==1){
             pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                    Sort.Direction.DESC, "createTime");
        }else  if (qo.getOrderType()==2){
             pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                    Sort.Direction.DESC, "viewnum");
        }else {
             pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                    Sort.Direction.ASC, "_id");
        }



        /*Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");*/
        Page<Travel> page = DBHelper.query(template, Travel.class, query, pageable);

        for (Travel travel : page) {
            UserInfo info = userInfoService.getOne(travel.getUserId());
            travel.setAuthor(info);
        }
        return  page ;
    }


    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void changeState(String id, int state) {
        //1:满足什么条件才进行审核
        Travel travel = this.get(id);
        if (travel == null) {
            throw new LogicException("参数异常");
        }

        if (travel.getState() != Travel.STATE_WAITING) {

            throw new LogicException("游记不是审核状态,不嫩进行审核");
        }

        if (state == Travel.STATE_RELEASE) {
            travel.setState(Travel.STATE_RELEASE);
            travel.setReleaseTime(new Date());
            travel.setLastUpdateTime(new Date());
            this.Update(travel);
        } else if (state == Travel.STATE_REJECT) {
            travel.setState(Travel.STATE_REJECT);
            travel.setReleaseTime(null);
            travel.setLastUpdateTime(new Date());
            this.Update(travel);
        } else {
            throw new LogicException("状态不对");
        }

    }


    @Override
    public List<Travel> list() {
        return repository.findAll();
    }

    @Override
    public void save(Travel travel) {
        travel.setId("");
        repository.save(travel);

    }

    @Override
    public void Update(Travel travel) {
        repository.save(travel);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        Destination destination = destinationService.getOne(travel.getDestId());
        travel.setDestName(destination.getName());
        travel.setLastUpdateTime(new Date());
        if (StringUtils.hasLength(travel.getId())){
            this.Update(travel);
        }else {
            travel.setCreateTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public List<Travel> travelsViewnumTop3(String destId) {

        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        List<Travel> list = repository.findByDestId(destId, pageable);


        return list;
    }

    @Override
    public List<Travel> queryByDestName(String keyword) {
        List<Travel> travels = repository.queryByDestName(keyword);
        for (Travel travel : travels) {
            travel.setAuthor(userInfoService.getOne(travel.getUserId()));
            Destination dest = destinationService.getOne(travel.getDestId());
            travel.setDest(dest);
        }
        return travels;
    }


}
